<?php
session_start();
include('config.inc.php');
// config session ให้เหมือนเข้าสู่ระบบแล้ว
// ตรวจสอลก่อนว่าเป็นผู้ดำเนินการหรือไม่
$getuser = $_GET['process'];
$no = $_GET['no'];
$sql_status = "";
//echo "[".$getuser."]";
if($getuser != ""){
	$numget = mysql_num_rows(mysql_query("SELECT * FROM users WHERE USERNAME = '$getuser'"));
	$get = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE USERNAME = '$getuser'"));
	//echo "[".$numget."]";
		if( $numget != 0){
			$_SESSION['sess_id'] = session_id();
			$_SESSION['sess_user'] = $getuser;
			$_SESSION['sess_status'] = "active";
			$_SESSION['sess_flag'] = $get['flag'];
			$sql_status = "select status from detail where no='$no'";
			//echo "[".$sql_status."]";
			$getstatus = mysql_fetch_array(mysql_query($sql_status));
			if($getstatus[status] == 1){
				$sql_up = "update detail set status='2' where no='$no'";
				mysql_query($sql_up);				
			}
		}
}
// session variable
$sess_id=$_SESSION['sess_id'];
$sess_user=$_SESSION['sess_user'];
$sess_status=$_SESSION['sess_status'];
$flag = $_SESSION['sess_flag'];

if ($sess_id<>session_id() || $sess_user=="" || $sess_status<>"active") {
		header( "Location: login.php");exit();
	}
//header
include('runid.php');
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=$charset\">";
echo "<title>$title</title>";
echo "<script language='javascript'src='js/script.js'></script>";
//ตัวแปร ที่ต้องใช้ 
$day_process = $Current_date;
//$username_process = "saksaws";
//$no = "test001-2552-0001";

$username_process = $_GET['process'];
$ac = $_GET['ac'];
//ดึงข้อมูลจากฐานข้อมูลเหตุเสีย มาแสดง
$sql = "SELECT * ,u.username as un_user, u.name as un_name,u.pnthai as un_pnthai, u.unit_code as un_unit_code,u.tel as un_tel,u.fax as un_fax,u.office as un_office
FROM detail d
left join fault_type f on d.fault_type = f.id
left join users u on d.username_note = u.username
left join location l on d.location_id = l.location_id
left join status_type s on d.status = s.status_id
where no = '$no'";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
$arr = mysql_fetch_array($result);
//ฟอร์มแสดงรายละเอียดการแจ้งเสีย
if($numrows != 0){

?>
<link href="style.css" rel="stylesheet" type="text/css" />
<table width="889" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td width="12%" style="vertical-align: top;padding:5px 0px 5px 10px;"><img src="images/logo_ttt.jpg" width=100 height=100></td>
          <td width="59%" valign="center"><h2 align="center">ใบแจ้งเหตุเสียอุปกรณ์สำนักงาน <?php //echo "[".$flag."]<br>";?></h2></td>
          <td width="29%" valign="bottom" style="vertical-align: bottom;padding:0px 10px 0px 0px;"><br>
              <div align="right">
                <p><small>เลขที่&nbsp;
                      <?=$no;?>
                </small></p>
                <p><small><?php echo thDate($arr['day_in']); ?>&nbsp;น.</small></p>
            </div></td>
        </tr>
        <tr>
          <td colspan="3"><hr width="98%">
          </td>
        </tr>
        <tr>
          <td colspan="3"><div style="vertical-align: top;padding:10px 0px 20px 60px;"><strong><small>รายละเอียดการแจ้งเสีย <?php //echo "[".$arr[status]."]";?></small></strong></div>
              <hr width="98%">
              <table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" >
                <tr>
                  <td width="19%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>เลขที่แจ้งเสีย <?php //echo "[".$$sql_status."]";?></small></strong></td>
                  <td width="14%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>ประเภทเหตุเสีย</small></strong></td>
                  <td width="20%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>สถานที่</small></strong></td>
                  <td width="26%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>เหตุเสีย</small></strong></td>
                  <td width="21%" style="vertical-align: top;padding:10px 0px 10px 10px;"><strong><small>สถานะการแจ้ง</small></strong></td>
                </tr>
                <tr>
                  <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['no']; ?></small></td>
                  <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['fault_name']; ?></small></td>
                  <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['location_id']."-".$arr['site_name']; ?></small></td>
                  <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['problem']; ?></small></td>
                  <td style="vertical-align: top;padding:10px 0px 10px 10px;"><small><? echo $arr['status_detail']; ?></small></td>
                </tr>
                <tr>
                  <td colspan="5" style="vertical-align: top;padding:10px 0px 10px 50px;"><small><strong>การแก้ปัญหา : </strong> <? if($arr[solution] == ""){echo "ยังไม่ได้รับการแก้ไข"; }else{ echo $arr['solution']; }?></small></td>
                </tr>
                <tr>
                  <td colspan="5" style="vertical-align: top;padding:10px 0px 10px 50px;"><p><small>รูปภาพประกอบ</small><br> 
                    <div style="padding:10px 0px 0px 30px; "><? if($arr[picture] == 'noimage.png' || $arr[picture] == ""){ 
					echo "<small><b>ไม่มีรูปประกอบ</b></small>"; 
					}else{ 
					echo "<a href=\"javascript:void(0);\"><img src=\"upload/files/noimage.png\" border='0' onClick=\"javascript:window.open('upload/files/$arr[picture]', '_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=auto, resizable=yes, copyhistory=no');\"></a>";
					}
					?>
                    </div>
                  </td>
                </tr>
                <tr>
                  <td colspan="5"><hr width="80%">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" >
                      <tr>
                        <td colspan="2" style="vertical-align: top;padding:10px 0px 10px 20px;"><strong><small>ผู้แจ้งเหตุเสีย</small></strong><small> : <? echo $arr['un_name']; ?></small></td>
                        <td width="25%" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>โทรศัพท์</strong> : <?php echo $arr['un_tel']; ?></small></td>
                        <td width="20%" style="vertical-align: top;padding:10px 0px 10px 20px;"><strong><small>โทรสาร</small></strong><small> : <? echo $arr['un_fax']; ?></small></td>
                        <td width="27%" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>อีเมลล์</strong> : <? echo $arr['un_user']."@ttt.co.th"; ?></small></td>
                      </tr>
                      <tr>
                        <td colspan="2" style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>หน่วยงาน</strong> : <? echo $arr['un_office']; ?></small></td>
                        <td style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>จังหวัด</strong> : <? echo $arr['un_pnthai']; ?></small></td>
                        <td style="vertical-align: top;padding:10px 0px 10px 20px;"><small><strong>unit_Code</strong> : <? echo $arr['un_unit_code']; ?></small>&nbsp;</td>
                        <td style="vertical-align: top;padding:10px 0px 10px 20px;">&nbsp;</td>
                      </tr>
                    </table></td>
                </tr>
              </table>
  <!--<div id="stPro" align="center"></div>-->
            <? 
			if($username_process <> "" && $flag == 2){
				if(($ac == "pro" && $arr[status] == 2) || $arr[status] == 2){
					echo "<div style='vertical-align: top;padding:0px 50px 0px 0px;' align='right'><small>วันที่ ";
					echo thDate($day_process);
					echo "</small></div>";
					echo "<div style='vertical-align: top;padding:10px 0px 20px 60px;'><strong><small>รายการดำเนินการ</small></strong></div>";
					echo "<div style='vertical-align: top;padding:0px 0px 0px 90px;'><small>
					<input type='button' name='bfault' id='bfault' onClick=\"javascript:showFaultPage('formfaultprocess.php')\" class='butsubmit' value='บันทึกการแก้ปัญหาเหตุเสีย' >
					<input type='button' name='bapp' id='bapp' onClick=\"javascript:showFaultPage('formapprove.php')\" class='butsubmit' value='ทำรายการขออนุมัติ'>
					</small></div>";
				}else if(($ac == "com" && $arr[status] == 4)|| $arr[status] == 4 ||$arr[status] == 6 ){
					echo "<div style='vertical-align: top;padding:0px 50px 0px 0px;' align='right'><small>วันที่ ";
					echo thDate($day_process);
					echo "</small></div>";
					echo "<div style='vertical-align: top;padding:10px 0px 20px 60px;'><strong><small>รายการดำเนินการ</small></strong></div>";
					echo "<div style='vertical-align: top;padding:0px 0px 0px 90px;'><small>
					<input type='button' name='bfault' id='bfault' onClick=\"javascript:showFaultPage('formfaultprocess.php')\" class='butsubmit' value='บันทึกการแก้ปัญหาเหตุเสีย' >
					<input type='button' name='bapp' id='bapp' onClick=\"javascript:showFaultPage('formapprove.php')\" class='butsubmit' value='ทำรายการขออนุมัติ' disabled='true'></small></div>";
				}else if($arr[status] == 3){
					echo "<div style='vertical-align: top;padding:0px 50px 0px 0px;' align='right'><small>วันที่ ";
					echo thDate($day_process);
					echo "</small></div>";
					echo "<div style='vertical-align: top;padding:10px 0px 20px 60px;'><strong><small>รายการดำเนินการ</small></strong></div>";
					echo "<div style='vertical-align: top;padding:0px 0px 0px 90px;' align='center'><small><h2>อยู่ระหว่างการขออนุมัติ</h2></small></div>";
				}
				echo "<hr width=\"98%\">";
			}
			
			?>
              <div id="formfault"></div>
		  </td>
        </tr>
        <tr>
          <td colspan="3">
		  <div id='uview' align='center' style='padding-top:20px;'>
<?php
	if($username_process == '' || $ac == ''){
		echo "<a href=\"javascript:window.close();\" class='b-link'><small>ปิดหน้าต่างนี้</small></a><br><br>";
	}
?>
</div></td>
        </tr>
    </table></td>
  </tr>
</table>

<?
}else{
	echo "<center><small><font color='red'>ไม่มีขอมูลเกี่ยวกับ หมายเลขการแจ้งเสียของคุณ</font><br><a href='index.php' style='text-decoration:none' class='b-link'>คลิกเพื่อกลับไปหน้าแรก</a></small></center>";
}
mysql_close($connect);
?>

<p>&nbsp;</p>
<div align="center">
<script language="javascript">

function doAppFault(){
	var appdetail = document.app.appdetail.value;
	var appcost = document.app.appcost.value;
	var app_user = document.app.app_user.value;
	if(appdetail == ""){
		alert('กรุณใส่รายละเอียดด้วยครับ');
		document.app.appdetail.focus();
	}else if(appcost == ""){
		alert('กรุณาใส่จำนวนเงินด้วยครับ \n หากไม่มีให้ใส่ 0');
		document.app.appcost.focus();
	}else if(app_user == ""){
		alert('กรุณาเลือกผู้อนุมัติด้วยครบ');
		document.app.app_user.focus();
	}else{
	document.getElementById("app_submit").disabled = true;
	// เรียกค่าจากตัวแปร php
	var no = "<?=$no;?>";
	var userpro = "<?=$username_process;?>";
	// เริ่ม Ajax
	//alert(no);
	//alert(userpro);
	var xmlHttp = Inint_AJAX();
	var url = 'addapprove.php';
	var pmeters = "no="+no+"&username_process="+userpro+"&appdetail="+encodeURI(document.getElementById("appdetail").value)+"&appcost="+
	encodeURI(document.getElementById("appcost").value)+"&app_user="+
	encodeURI(document.getElementById("app_user").value);
	//alert(pmeters);
	document.getElementById('bapp').disabled = 'true';
	document.getElementById('bfault').disabled = 'true';
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 1){ document.getElementById("stPro").innerHTML = "Loading..."; }
		if(xmlHttp.readyState == 2){ document.getElementById("stPro").innerHTML = "Loading..."; }
		if(xmlHttp.readyState == 3){ document.getElementById("stPro").innerHTML = "Loading..."; }
		if(xmlHttp.readyState == 4){
				document.getElementById("stPro").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
	}
}
function doProcessFault(){
	var solution = document.profault.solution.value;
	var cost = document.profault.cost.value;
	if(solution == ""){
		alert('กรุณใส่รายละเอียดด้วยครับ');
		document.profault.solution.focus();
	}else if(cost == ""){
		alert('กรุณาใส่จำนวนเงินด้วยครับ \n หากไม่มีให้ใส่ 0');
		document.profault.cost.focus();
	}else{
	document.getElementById("faultsubmit").disabled = true;
	// เรียกค่าจากตัวแปร php
	var no = "<?=$no;?>";
	var userpro = "<?=$username_process;?>";
	// เริ่ม Ajax
	var xmlHttp = Inint_AJAX();
	var url = 'addprocess.php';
	var pmeters = "no="+no+"&username_process="+userpro+"&solution="+encodeURI(document.getElementById("solution").value)+"&cost="+
	encodeURI(document.getElementById("cost").value);
	//alert(pmeters);
	document.getElementById('bfault').disabled = 'true';
	document.getElementById('bapp').disabled = 'true';
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 1){
				document.getElementById("stPro").innerHTML = "Loading...";
			}
		if(xmlHttp.readyState == 2){
				document.getElementById("stPro").innerHTML = "Loading...";
			}
		if(xmlHttp.readyState == 3){
				document.getElementById("stPro").innerHTML = "Loading...";
			}
		if(xmlHttp.readyState == 4){
				document.getElementById("stPro").innerHTML = xmlHttp.responseText;
		}
	};
	xmlHttp.open('POST',url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(pmeters);
	
	}
}
//popup center 
function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=auto, resizable=yes, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

</script>
</div>
<style type="text/css">
.b-link{
	border:1px solid #999966;
	color: #FFFFFF;
	font-size:10pt;
	font-weight:bold;
	background: #999999;
	margin:0px;
	padding:5px 20px 5px 20px;
	text-decoration:none;
}
.b-link:hover{
	background: #3399FF;
}
</style>
